# sort functions
_modifiedDateSorter = lambda a,b: cmp(
    a.rdf_join('dctermsModified') or a.rdf_join('dcCreated'),
    b.rdf_join('dctermsModified') or b.rdf_join('dcCreated')
)

def ModifiedDateSorter(keywords):
    return _modifiedDateSorter


_createdDateSorter = lambda a,b: cmp(
    a.rdf_join('dcCreated'),
    b.rdf_join('dcCreated')
)

def CreatedDateSorter(keywords):
    return _createdDateSorter


_titleSorter = lambda a,b: cmp(
    a.rdf_join('dcTitle'),
    b.rdf_join('dcTitle')
)

def TitleSorter(keywords):
    return _titleSorter


_nick = lambda n: n.rdf_join('foafNick')
_modifierNickSorter = lambda a,b: cmp(
    u''.join(map(_nick, a.mLast_Modifier)) or \
    u''.join(map(_nick, a.dcCreator)),
    u''.join(map(_nick, b.mLast_Modifier)) or \
    u''.join(map(_nick, b.dcCreator))
)

def ModifierNickSorter(keywords):
    return _modifierNickSorter


_creatorNickSorter = lambda a,b: cmp(
    u''.join(map(_nick, a.dcCreator)),
    u''.join(map(_nick, b.dcCreator))
)

def CreatorNickSorter(keywords):
    return _creatorNickSorter


_str_count = unicode.count
def RevelanceSorter(keywords):
    def _cmp(a,b):
        join = a.rdf_join
        r = _str_count(join('dcTitle'),keywords) * 0.5 + \
            _str_count(join('dcSubject'),keywords) * 0.25 + \
            _str_count(join('dcDescription'),keywords) * 0.25
        join = b.rdf_join
        return cmp(r, _str_count(join('dcTitle'), keywords) * 0.5 + \
            _str_count(join('dcSubject'), keywords) * 0.25 + \
            _str_count(join('dcDescription'), keywords) * 0.25
        )
    return _cmp